package cn.kailangtu.dataspot.data;

import cn.kailangtu.commontools.CommonUtils;
import cn.kailangtu.commontools.Loggers;

public class DataBeanEx {
    private int value = 0;
    private int address = -1;
    private boolean isChanged = false;
    private int wordCount = 1;

    public DataBeanEx(int address)
    {
        this.address = address;
    }
    public DataBeanEx(int address,int wordCount)
    {
        this.address = address;
        this.wordCount = wordCount;
    }

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        if(this.value != value) {
            Loggers.errInfo("new value:"+value);
            this.value = value;
            isChanged = true;
        }
    }

    public int getAddress() {
        return address;
    }


    public void processData故障数据1(byte[] data)
    {
        if(address < 4500)
        {
            isChanged = false;
            int off =  (address - 3499) * 2;
            int index = 3+ off;
            if(wordCount == 1) {
                if (data.length > index + 1) {
                    setValue(CommonUtils.byteArrayToInt16(new byte[]{data[index], data[index + 1]}));
                }
            }
            else if(wordCount == 2)
            {
                if (data.length > index + 3) {
                    setValue(CommonUtils.byteArrayToInt(new byte[]{data[index+2],data[index+3],data[index], data[index + 1]}));
                }
            }

        }
    }

    public void processData4500(byte[] data){
        if(address >= 4500){
            isChanged = false;
            int off =  (address - 4500) * 2;
            int index = 3+ off;
            if(wordCount == 1) {
                if (data.length > index + 1) {
                    setValue(CommonUtils.byteArrayToInt16(new byte[]{data[index], data[index + 1]}));
                }
            }
            else if(wordCount == 2)
            {
                if (data.length > index + 3) {
                    setValue(CommonUtils.byteArrayToInt(new byte[]{data[index], data[index + 1],data[index+2],data[index+3]}));
                }
            }
        }
    }

    public boolean isChanged() {
        return isChanged;
    }


    public int getWordCount() {
        return wordCount;
    }

    public void setWordCount(int wordCount) {
        this.wordCount = wordCount;
    }
}
